<?php
namespace Admin\Controller;
use Think\Controller;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/25
 * Time: 14:49
 */
class BasescoreController extends CommonController
{
    private $model;
    public function __construct()
    {
        parent::__construct();
        $this->model = D('Basescore');
    }
    /**
     * 
    * 应用列表
    * @author wang
    * @param 
    */
    public function basescoreList()
    {
        $where = array();
        $sort = I('request.sort', 'orderid');
        $order = I('request.order', 'asc');
        $basescore = I('basescore');
        if(!empty($basescore)) $where['b.basenum|m.mode|g.game'] = array('like','%'.$basescore.'%');
        $result = M('basescore as b')
                ->join('LEFT JOIN '.C('DB_PREFIX').'mode as m ON b.modeid = m.modeid')
                ->join('LEFT JOIN '.C('DB_PREFIX').'game as g ON m.gameid = g.gameid')
                ->field('b.*,m.mode,m.gameid,g.game')
                ->where($where)
                ->order($sort." ".$order)
                ->select();
        foreach ($result as $k => &$v) {
            $v['modeid']  = $v['modeid'] ?  $v['modeid'] : '';
            $v['createtime']=date("Y-m-d H:i:s",$v['createtime']);
            $v['updatetime']=date("Y-m-d H:i:s",$v['updatetime']);
            $v['orderid']=(int)$v['orderid'];
        }
        $this->ajaxReturn($result);
    }

    /**
     * 
    * 根据游戏显示模式
    * @author wang
    * @param 
    */
    public function modeList(){
        $gameid = $_GET['gameid'];
        $list = M('mode')->where("gameid='$gameid'")->select();
        $this->ajaxReturn($list);
    }

    /**
    * 增加difen
    * @author wang
    * @param 
    */
    public function addBasescore(){
    	$baseid = I('baseid');
        $data = I();
        $data['admin'] = session('admin');
        $data['updatetime'] = time();
    	if($baseid){
    		if($this->model->where("baseid='$baseid'")->save($data)){
            	$this->ajaxReturn(array('success' => 1, 'msg' => '用户修改成功','id'=>$baseid));
	        }else{
	            $this->ajaxReturn(array('success' => 0, 'msg' => '用户修改失败'));
	        }
    	}else{
            // $data['id'] = (new  \Org\Twitter\IdWork(31,31))->nextId();
            $data['createtime']  = time();
    		if($this->model->add($data)){
            	$this->ajaxReturn(array('success' => 1, 'msg' => '用户新增成功'));
	        }else{
	            $this->ajaxReturn(array('success' => 0, 'msg' => '用户增加失败'));
	        }
    	}
    }

    /**
    * 删除difen 
    * @author wang
    * @param 
    */
    public function delBasescore(){
        $baseid = I('baseid');
        // $sgame = M('GameServer')->where("basescoreid='$baseid'")->select();
        // if($sgame){
        //     $result = array(
        //       'sta' => 0, 
        //       'msg' => '无法删除'
        //     );
        // }else{
            if($this->model->where("baseid='$baseid'")->delete()){
                $result = array(
                    'sta' => 1, 
                    'msg' => '删除成功'
                );
            }else{
                $result = array(
                    'sta' => 0, 
                    'msg' => '删除失败'
                ); 
            }
        // }
        $this->ajaxReturn($result);
    }

}